﻿using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Autofac;
using Pay.Data;
using Pay.DTO;
using Pay.Service;
using Pay.Service.Cache;
using Pay.Web.Utils;
using Pay.IService;

namespace Pay.Web {
	public class Global : HttpApplication {
		protected void Application_Start() {
			DbContextInitailizer.Initialize();
			DependencyInjectionConfig.Register();
			AutoMapperConfig.Register();
			FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
			AreaRegistration.RegisterAllAreas();
			RouteConfig.RegisterRoutes(RouteTable.Routes);
			BundleConfig.RegisterBundles(BundleTable.Bundles);


            #region 初始化定时同步任务
            //var schedulerBuilder = new ContainerBuilder();
            //schedulerBuilder.RegisterType<TokenScheduler>();
            //schedulerBuilder.RegisterType<HniaoScheduler>();
            //schedulerBuilder.RegisterType<BillScheduler>();
            //using (var TokenContainer = schedulerBuilder.Build()) {
            //    var tokenScheduler = TokenContainer.Resolve<TokenScheduler>();
            //    var hniaoScheduler = TokenContainer.Resolve<HniaoScheduler>();
            //    var billScheduler = TokenContainer.Resolve<BillScheduler>();
            //    FluentScheduler.JobManager.Initialize(tokenScheduler, hniaoScheduler, billScheduler);
            //}
            #endregion
        }


        protected void Application_End() {
            //IIS应用程序池回收后立即触发 Application_Start，以维持定时同步任务
            System.Threading.Thread.Sleep(600);
            string url = t_RequestMethod.web_url;
            System.Net.HttpWebRequest myHttpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            System.Net.HttpWebResponse myHttpWebResponse = (System.Net.HttpWebResponse)myHttpWebRequest.GetResponse();
            System.IO.Stream receiveStream = myHttpWebResponse.GetResponseStream();
        }
    }
}
